package br.uefs.dexa.rfframework.strategies.lire;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

import net.semanticmetadata.lire.DocumentBuilder;
import net.semanticmetadata.lire.DocumentBuilderFactory;
import net.semanticmetadata.lire.utils.FileUtils;
import net.semanticmetadata.lire.utils.LuceneUtils;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

import br.uefs.dexa.rfframework.config.AppConfigManager;
import br.uefs.dexa.rfframework.db.DatabaseManager;
import br.uefs.dexa.rfframework.db.map.MapDatabaseManager;
import br.uefs.dexa.rfframework.gui.GUIActionListener;
import br.unicamp.ic.lis.rfframework.impl.interfaces.ICollectionInitializationStrategy;
import br.unicamp.ic.lis.rfframework.impl.interfaces.IUserActionListener;
import br.unicamp.ic.lis.rfframework.objects.DigitalObject;
import br.unicamp.ic.lis.rfframework.objects.DigitalObjectSet;




public class LireCollectionInitializer implements ICollectionInitializationStrategy {
	
	
	IUserActionListener actionListener = new GUIActionListener();
	List<DigitalObject> list = new ArrayList<DigitalObject>();
	
	
	
	@Override
	public DigitalObjectSet<DigitalObject> run() {
		
		for (int i = 16010; i < 20000; i++) {
			DigitalObject object = new DigitalObject((long) i);
			list.add(object);
		}
		
//		createCEDDIndex();
		
		
		
		return ramdomize(list, false);
	}
	
	private void createCEDDIndex()
	{
		   // Getting all images from a directory and its sub directories.
	      ArrayList<String> images;
	      
	      images = MapDatabaseManager.getInstance().getImagePaths();

	      // Creating a CEDD document builder and indexing all files.
	      DocumentBuilder builder = DocumentBuilderFactory.getCEDDDocumentBuilder();
	      // Creating an Lucene IndexWriter
	      IndexWriterConfig conf = new IndexWriterConfig(LuceneUtils.LUCENE_VERSION,
	              new WhitespaceAnalyzer(LuceneUtils.LUCENE_VERSION));
	      IndexWriter iw;
			try {
				String indexFile = AppConfigManager.getInstance().getLireIndexFile();
				//iw = new IndexWriter(FSDirectory.open(new File(INDEX_FILE)), conf);
				iw = new IndexWriter(FSDirectory.open(new File(indexFile)), conf);
			} catch (IOException e1) {
				e1.printStackTrace();
				return;
			}
	      // Iterating through images building the low level features
	      for (Iterator<String> it = images.iterator(); it.hasNext(); ) {
	          String imageFilePath = it.next();
	          System.out.println("Indexing " + imageFilePath);
	          try {
	              BufferedImage img = ImageIO.read(new FileInputStream(imageFilePath));
	              Document document = builder.createDocument(img, imageFilePath);
	              iw.addDocument(document);
	          } catch (Exception e) {
	              System.err.println("Error reading image or indexing it.");
	              e.printStackTrace();
	          }
	      }
	      // closing the IndexWriter
	      try {
				iw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	      System.out.println("Finished indexing.");
   
	}
	
	private static DigitalObjectSet<DigitalObject> ramdomize(List<DigitalObject> list, boolean limitNumberOfObjects)
	{
		DigitalObjectSet<DigitalObject> collection = new DigitalObjectSet<DigitalObject>();
		long seed = System.nanoTime();
		Collections.shuffle(list, new Random(seed));
		int numberOfObjects;
		
		if(limitNumberOfObjects)
			numberOfObjects = AppConfigManager.getInstance().getNumberOfRetrievedObjectsShown();
		else
			numberOfObjects = list.size();
		
		for (int i = 0; i < numberOfObjects; i++) {
			collection.addObject(new DigitalObject(list.get(i).getId()));
		}
		
		return collection;
	}
	

}

